from flask import Flask, request, render_template, current_app
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy
from flask_session import Session
from moldels import db
from utils.common import total_price, get_count_and_prices, get_order_status
from utils.hookfunction import set_after_request_handle_fuc
from views import commod_blu, user_blu, admin_blu


app = Flask(__name__)

# 加载配置
app.config.from_pyfile("config.py")


# 创建Session对象（此时app这个flask对象中生成的session信息才会存储到Redis里）
Session(app)

db.init_app(app)
# 注册蓝图
app.register_blueprint(commod_blu)
app.register_blueprint(user_blu)
app.register_blueprint(admin_blu)

# 添加过滤器
app.add_template_filter(total_price)
app.add_template_filter(get_count_and_prices)
app.add_template_filter(get_order_status)

# 添加数据库迁移等工具
manager = Manager(app)
# 生成migrate对象 用来数据库迁移
migrate = Migrate(app, db)
# 添加db命令
manager.add_command('db', MigrateCommand)


# 钩子函数
set_after_request_handle_fuc(app)



if __name__ == '__main__':
    # app.run(port=8899)
    manager.run()
